#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root)
    {
        if (root->left == nullptr && root->right == nullptr)return true;
        return dfs(root);
    }
    bool dfs(TreeNode* root)
    {

        if (root == nullptr)return true;
        if (dfs(root->left) == false) return false;
        if (root->val > prev)
        {
            prev = root->val;
        }
        else return false;
        if (dfs(root->right) == false) return false;
        //dfs(root->right);
        return true;
    }

private:
    long prev = LONG_MIN;
};